package org.gesplan.listeners;

import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.gesplan.ui.MaterialBuildingPanel;
import org.gesplan.util.Utils;

/**
 *
 * @author David
 */
public class SelectionListener implements ListSelectionListener{
    
    private final JTable table;
    private MaterialBuildingPanel mbp;

    
    public SelectionListener(JTable table) {
        this.table = table;
    }

    public SelectionListener(JTable table, MaterialBuildingPanel mbp) {
        this.table = table;
        this.mbp = mbp;
    }    

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
            // If cell selection is enabled, both row and column change events are fired
            if (e.getSource() == table.getSelectionModel() && table.getRowSelectionAllowed()) {
                Integer current = (Integer) table.getSelectedRow() + 1;
                Integer total = table.getRowCount();                
                Utils.setRecordInfo(current.toString(), total.toString());
                if (mbp != null){
                    mbp.updateTotal();
                }
            }
        }
    }
    
    
}